/****************************************************************************
 * org_mpris_mpd.cpp
 *  Copyright © 2010, Vsevolod Velichko <torkvema@gmail.com>.
 *  Licence: GPLv3 or later
 *
 ****************************************************************************
 *                                                                          *
 *   This library is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by   *
 *   the Free Software Foundation; either version 3 of the License, or      *
 *   (at your option) any later version.                                    *
 *                                                                          *
 ****************************************************************************/

#include "mpris.h"
#include "mediaplayer2adaptor.h"
#include "playeradaptor.h"
#include <QByteArray>
#include <QString>
#include <QStringList>
#include <QVariant>
#include <QVariantList>
#include <qdebug.h>

MprisAdaptor::MprisAdaptor() :
    QObject()
{
	qDebug() << "MprisAdaptor created";
    new MediaPlayer2Adaptor(this);
    new PlayerAdaptor(this);

    QDBusConnection::sessionBus().registerObject("/org/mpris/MediaPlayer2", this);
    QDBusConnection::sessionBus().registerObject("/org/mpris/MediaPlayer2/Player", this);
    player.show();
    MediaKeyAdaptor *mediakey = new MediaKeyAdaptor("org.gnome.SettingsDaemon","/org/gnome/SettingsDaemon/MediaKeys",QDBusConnection::sessionBus(),0);
    mediakey->GrabMediaPlayerKeys(QCoreApplication::applicationName(),0);
    QObject::connect(mediakey,SIGNAL(MediaPlayerKeyPressed(QString,QString)),&player,SLOT(MediaPlayerKeyPressed(QString,QString)));
}

// org.mpris.MediaPlayer2 interface

bool MprisAdaptor::canQuit () const
{
    return true;
}

bool MprisAdaptor::canRaise () const
{
    return false;
}

bool MprisAdaptor::hasTrackList () const
{
    return false;
}

QString MprisAdaptor::identity () const
{
    return QCoreApplication::applicationName();
}

QString MprisAdaptor::desktopEntry () const
{
    return QCoreApplication::applicationName().toLower(); //название файла .desktop, без расширения и пути.
    //!!! ОБЯЗАТЕЛЬНО НАЛИЧИЕ Данной строки
}

QStringList MprisAdaptor::supportedMimeTypes () const
{
	// TODO
	static QStringList res = QStringList() 
        << "audio/mp3";
	return res;
}

QStringList MprisAdaptor::supportedUriSchemes () const
{
    static QStringList res; //Хз че писать...
    res.append("");
	return res;
}

void MprisAdaptor::Quit ()
{

}
void MprisAdaptor::Raise ()
{

}

// org.mpris.MediaPlayer2.Player interface

bool MprisAdaptor::canControl () const
{
    return true;
}

bool MprisAdaptor::canGoNext () const
{
    return true; //ВОЗМОЖНО ПРИДЕТСЯ ЗАПРЕТИТЬ canGoNext (), например если игрет последняя песня в списке...
    //Зависет от устрайтсва пллера
}

bool MprisAdaptor::canGoPrevious () const
{
    return true;
}

bool MprisAdaptor::canPause () const
{
    return true;
}

bool MprisAdaptor::canPlay () const
{
    return true;
}

bool MprisAdaptor::canSeek () const
{
    return true;
} //TODO

QString MprisAdaptor::loopStatus () const
{
    //return mpdCon->getRepeat();
    return "";
}

void MprisAdaptor::setLoopStatus (const QString &status)
{
    //mpdCon->setRepeat(status);
}

double MprisAdaptor::maximumRate () const
{
    return 10;
}

double MprisAdaptor::minimumRate () const
{
    return 1;
}

double MprisAdaptor::rate () const
{
    return 1.0;
}

void MprisAdaptor::setRate (double)
{
    qDebug("set rate");
}

QVariantMap MprisAdaptor::metadata () const 
{
    QVariantMap map;
    map.insert("TEST SONG", QVariant("SONG INFO"));
    return map;
}

QString MprisAdaptor::playbackStatus () const
{
    return "play";
}

bool MprisAdaptor::shuffle () const
{
    return true;
}

void MprisAdaptor::setShuffle (bool value)
{
    qDebug("setShuffle();");
}

double MprisAdaptor::volume () const
{
    return 100.0;
}

void MprisAdaptor::setVolume (double value)
{
    qDebug("SET VOLUME");
}

void MprisAdaptor::Next ()
{
    qDebug("NEXT");
    player.on_bt_next_clicked();
}
void MprisAdaptor::Pause ()
{
    qDebug("PAUSE");
}
void MprisAdaptor::Play ()
{
    qDebug("PLAY");
}
void MprisAdaptor::PlayPause ()
{
    qDebug("PLAY//PAUSE");
    player.on_bt_play_clicked();
}
void MprisAdaptor::Previous ()
{
    player.on_bt_previous_clicked();
}
void MprisAdaptor::Stop ()
{
    player.on_bt_stop_clicked();
}

void MprisAdaptor::OpenUri (const QString &/*Uri*/) //TODO
{

}

qlonglong MprisAdaptor::position () const
{
    return 1000;
}

void MprisAdaptor::Seek (qlonglong offset) 
{

}
void MprisAdaptor::SetPosition (const QDBusObjectPath &/*TrackId*/, qlonglong /*Position*/) //TODO
{
    qDebug("SET POSITION");
}

